import 'package:flutter/material.dart';
import '../../models/home_info_model.dart';
import '../../manger/cached_image_widget.dart';

typedef HotItemTapCallback = void Function(int subId);

class HomeHotView extends StatelessWidget {
  final List<HotCategoryListModel> hotCategoryList;
  final HotItemTapCallback onItemTap;

  const HomeHotView({
    super.key,
    required this.hotCategoryList,
    required this.onItemTap,
  });

Widget build(BuildContext context) {
  return Container(
    margin: const EdgeInsets.symmetric(horizontal: 15),
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(10),
    ),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          height: 410,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: hotCategoryList.length,
            itemBuilder: (context, index) {
              final category = hotCategoryList[index];
              return Container(
                width: 155,
                margin: const EdgeInsets.only(right: 15),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12), // 设置圆角
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(left: 15, top: 10),
                      child: Text(
                        category.tag,
                        style: const TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    Expanded(
                      child: GridView.builder(
                        physics: const NeverScrollableScrollPhysics(),// 禁止滚动
                        padding: EdgeInsets.symmetric(horizontal: 15),
                        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          mainAxisSpacing: 15,
                          crossAxisSpacing: 12,
                          childAspectRatio: 1,
                        ),
                        itemCount: category.children.length.clamp(0, 10),
                        itemBuilder: (context, childIndex) {
                          final child = category.children[childIndex];
                          return SizedBox(
                            width: 56,
                            height: 56,
                            child: GestureDetector(
                              onTap: () {
                                onItemTap(child.id);
                              },
                              child: SizedBox(                
                                 child: ClipRRect(
                                    borderRadius: BorderRadius.circular(10),
                                    child: CachedImage(
                                      imageUrl: child.pic,
                                      fit: BoxFit.cover,
                                    ),
                                  ),
                              ),
                            ),
                          );
                        },
                      ),
                    ),
                  ],
                ),
              );
            },
          ),
        ),
      ],
    ),
  );
}

}